layui.use(['element', 'layer'], function(){
    var element = layui.element;
    var layer = layui.layer;
    var $ =layui.jquery;

    //指定选项卡tab
    var tab = {
        tabAdd:function (title,url,id) {
          //添加一个tab
          element.tabAdd('index_tab',{
             title:title,
             content:'<iframe tab-id="'+id+'" frameborder="0" src="'+url+'" scrolling="yes" class="x-iframe"></iframe>',
             id:id
          });
        },
        //删除指定Tab项
        tabDelete:function (th) {
            element.tabDelete('index_tab','44');
            th.addClass('layui-btn-disabled');
        },
        //切换到指定Tab项
        tabChange: function(id){
            element.tabChange('index_tab', id); //切换到：用户管理
        }

    };

    //左侧菜单控制
    $("#menu-content .pure-content .nav li").click(function (event) {
        var title =$(this).find('cite').html();
        var url = $(this).find('a').attr('_href');
        var index=$("#menu-content .pure-content .nav li").index($(this));
        console.log("title:"+title+";url:"+url+";index:"+index);
        //禁止重复建立相同的选项卡tab
        for (var i = 0; i <$('.x-iframe').length; i++) {
            if($('.x-iframe').eq(i).attr('tab-id')==index+1){
                tab.tabChange(index+1);
                event.stopPropagation();
                return;
            }
        };
        //建立新选项卡tab
        tab.tabAdd(title,url,index+1);
        tab.tabChange(index+1);
        event.stopPropagation();
        //调整子页面iframe高度
        FrameWH();
        $(window).resize(function () {
            FrameWH();
        });
    });

    //控制iframe高度
    FrameWH();
    $(window).resize(function () {
        FrameWH();
    });
    function FrameWH() {
        var h = $(window).height()-44;
        $("iframe").css("height",h+"px");
    }
});